package com.dinadi.yidi.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.dinadi.yidi.R;
import com.dinadi.yidi.bean.ExhibitionreaRequestEntity;
import com.dinadi.yidi.utils.ImageUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * author: JiaYu.He
 * Date: 2017/3/24
 * Time: 12:53
 */

public class ExhibitionreaAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {

    private Context mContext;
    private List<ExhibitionreaRequestEntity> mDatas = new ArrayList<>();
    private String type;
    private String showId;
    /*运用典型的观察者模式添加点击事件*/
    /*定义接口变量*/
    private ExhibitionreaAdapter.OnRecyclerViewItemClickListener mOnItemClickListener = null;


    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, String data);
    }


    public void addAllData(List<ExhibitionreaRequestEntity> dataList) {
        this.mDatas.addAll(dataList);
        notifyDataSetChanged();
    }

    public void clearData() {
        this.mDatas.clear();
    }

    public ExhibitionreaAdapter(Context context) {
        mContext = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_exhibitionrea_item, parent, false);
        //将创建的View注册点击事件
        v.setOnClickListener(this);
        return new ItemViewHolder(v);


    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        ExhibitionreaAdapter.ItemViewHolder itemViewHolder = (ExhibitionreaAdapter.ItemViewHolder) holder;
//            String         str            = mDatas.get(position);
        itemViewHolder.text_msg.setText(mDatas.get(position).getTitle());
        String imgUrl = mDatas.get(position).getCoverImg();

        if (imgUrl != null) {
            if (!imgUrl.equals("")) {
                ImageUtils.loadImageView(mContext, imgUrl, itemViewHolder.imageView);
            } else {
                ImageUtils.loadImageView(mContext, R.drawable.symbols_bitmap, itemViewHolder.imageView);
            }
        }else{
            ImageUtils.loadImageView(mContext, R.drawable.symbols_bitmap, itemViewHolder.imageView);

        }
        type = mDatas.get(position).getType();//1展览  2站赛
        if (type.equals("1")) {
          //  itemViewHolder.img_type.setImageResource(R.drawable.exhibition_icon_exhibition);
            showId = mDatas.get(position).getShowId();
            holder.itemView.setTag(showId);
        } else {
         //   itemViewHolder.img_type.setImageResource(R.drawable.exhibition_icon_match);
            showId = mDatas.get(position).getShowId();
            holder.itemView.setTag(showId);
        }
//        itemViewHolder.imageView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent();
//                if (type.equals("1")) {
//                    intent.setClass(mContext, WorksExhibitionActivity.class);
//                } else if (type.equals("2")) {
//                    intent.setClass(mContext, ExhibitionMatchActivity.class);
//                }
//                intent.putExtra("showId", showId);
//                mContext.startActivity(intent);
//            }
//        });

    }


    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {

        TextView text_msg;
        ImageView imageView, img_type;

        public ItemViewHolder(View itemView) {
            super(itemView);
            text_msg = (TextView) itemView.findViewById(R.id.text_title);
            imageView = (ImageView) itemView.findViewById(R.id.cover_img);
            img_type = (ImageView) itemView.findViewById(R.id.img_type);
            //initListener(itemView);
        }

//        private void initListener(View itemView) {
//            itemView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Toast.makeText(mContext, "poistion " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
//                }
//            });
//        }
    }


    /*将事件转移给外面的调用者*/
    @Override
    public void onClick(View v) {

        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v, (String) v.getTag());

        }
    }

    /*最后暴露给外面的调用者*/
    public void setmOnItemClickListener(ExhibitionreaAdapter.OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;

    }

  public String getType(){
      return type;
  }
}

